package com.bdth.bdthiss.jsoup;

import com.bdth.bdthiss.util.MD5Utils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 京东商品数据详情爬取
 * Created by Administrator on 2020/4/18.
 */
public class JdTools {

    /**
     * 根据url下载商品图片
     * @param uid 商品编码
     * @param url 商品网络地址
     * @param saveWhere 保存的位置
     * @return
     * 例如 url = http://www.officemate.cn/product-391327.html
     *      uid = 391327
     *      saveWhere = "C:\\jsoup\\images\\"
     */
    public static Map<String,String> getHtmlAndDownPic(String uid, String url, String saveWhere) throws Exception{
        Map<String, String> result = new HashMap<>();
        String type = "";
        String brand = "";
        String html = "";
        try {
            System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
            ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions.addArguments("--headless");
            WebDriver driver = new ChromeDriver(chromeOptions);
            driver.get(url);
            Thread.sleep(3000);
            html = driver.getPageSource();
            driver.quit();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        //获取url的文档
        Document document = Jsoup.parse(html);
        Elements skuNameElement = document.select("div[class='sku-name']");
        String skuName = skuNameElement.text();
        System.out.println("商品名称===="+skuName);
        //第一步：下载商品的展示图片thumbnail
        FileTools.createDir(saveWhere+"jd\\"+uid+"\\thumbnail\\");
        Elements div = document.select("div[id='spec-list']");
        Document divDoc = Jsoup.parse(div.toString());
        Elements thumbnails = divDoc.select("img");
        File picsn = new File(saveWhere+"jd\\"+uid+"\\thumbnail\\picsn.txt");
        picsn.createNewFile();
        Writer picsnWriter = new FileWriter(picsn);
        for(Element thumbnail : thumbnails){
            String src = thumbnail.select("img").attr("data-url");
            //下载展示图片
            //京东大图片格式
            String surl = "http://img11.360buyimg.com/n1/s450x450_";
            String fileName = FileTools.downPic(FileTools.getRealUrl(surl+src),saveWhere+"jd\\"+uid+"\\thumbnail");
            String fileMD5 = MD5Utils.getFileMD5String(new File(saveWhere+"jd\\"+uid+"\\thumbnail\\"+fileName));
            String value = fileMD5+","+"upload/jd/"+uid+"/thumbnail/"+fileName+"\r\n";
            picsnWriter.append(value);
        }
        //下载详细内容
        //第二步：下载商品详细内容
        //搜索到商品详情内容id=J-detail-content
        String resultHtml = "<div>";
        Element jdcontent = document.getElementById("J-detail-content");
        Elements style = jdcontent.select("style");
        //resultHtml = display.toString();
        Pattern pattern = Pattern.compile("url[(](.*?)[)]");
        String strStyle = style.toString();
        Matcher matcher = pattern.matcher(strStyle);
        String imgurl = "";
        while(matcher.find()){
            imgurl = matcher.group(1);
            System.out.println(imgurl);
            //下载图片
            FileTools.downPic(FileTools.getRealUrl(imgurl),saveWhere+"jd\\"+uid);
            //保存为新的网络路径
            String newSrc = "/upload/jd/"+uid+"/"+FileTools.getFileName(imgurl);
            //组建img详情
            String imgTemp = "<img src='"+newSrc+"'><br>";
            resultHtml += imgTemp;
        }
        resultHtml += "</div>";
        picsnWriter.close();
        System.out.println(resultHtml);
        result.put("html",resultHtml);
        result.put("skuname",skuName);
        return result;
    }
    /**
     * 测试方法
     * @param args
     */
    public static void main(String[] args) {
        try {
            JdTools.getHtmlAndDownPic("100001966814","https://item.jd.com/100001966814.html","C:\\jsoup\\images\\");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
